<template>
  <div style="padding: 10px">
    <el-table :data="tableData"   stripe style="width: 100%">
      <!--      <el-table-column fixed prop="id" sortable label="ID" width="150"/>-->



      <el-table-column label="订单号" width="240">
        <template #default="scope" >
          <el-button type="text" @click="getOrderDetail(scope.row)"
          >{{scope.row.orderId}}</el-button
          >
        </template>
      </el-table-column>

      <el-table-column label="订单状态" width="120" >
        <template #default="scope">
          <view v-if="scope.row.flag==0"><el-button type="info"  >待处理</el-button></view>
          <view v-if="scope.row.flag==1"><el-button type="primary"  >已发货</el-button></view>
          <view v-if="scope.row.flag==2"><el-button type="warning"  >暂存（待补发货）</el-button></view>
          <view v-if="scope.row.flag==3"><el-button type="danger"  >已退货</el-button></view>
          <view v-if="scope.row.flag==4"><el-button type="success"  >已确认收货</el-button></view>
        </template>

      </el-table-column>

<!--      <el-table-column fixed="right" label="操作" width="200">-->
<!--        <template #default="scope">-->
<!--          <el-popconfirm title="确认删除?" @confirm="handleDelete(scope.row)">-->
<!--            <template #reference>-->
<!--              <el-button type="text">删除</el-button>-->
<!--            </template>-->
<!--          </el-popconfirm>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="240" style="align-self: center">
        <template #default="scope">
          <view v-if="scope.row.flag===0"><el-button type="primary"  >催单</el-button></view>
          <el-popconfirm v-if="scope.row.flag===1" title="确认退货?" @confirm="return_goods(scope.row)">
            <template #reference>
              <el-button type="primary">退货</el-button>
            </template>
          </el-popconfirm>
<!--          <view v-if="scope.row.flag===1"><el-button type="primary" @click="return_goods(scope.row)" >退货</el-button></view>-->
          <view v-if="scope.row.flag===2"><el-button type="primary"  >催单</el-button></view>
<!--          <view v-if="scope.row.flag===3"><el-button type="primary"  >评价</el-button></view>-->
          <view v-if="scope.row.flag===4&&scope.row.evaluate==null"><el-button type="primary" @click="pringjia2(scope.row)" >评价</el-button></view>
          <view v-if="scope.row.evaluate!=null"><el-button type="info"  >已评价</el-button></view>
          &nbsp;
          &nbsp;
          &nbsp;
<!--          <view v-if="scope.row.flag==1"><el-button type="primary"  >评价</el-button></view>-->
        </template>
      </el-table-column>



    </el-table>

    <el-dialog v-model="dialogFormVisible2" title="评价" width="50%">


      <el-form :model="form">
        <div class="demo-rate-block">
          <span class="demonstration">对我们的服务满意的话，就给个五星好评吧~~</span>
          <el-rate v-model="form.value" />
        </div>
        <el-input v-model="form.comment" type="textarea"/>
        &nbsp;<el-button type="primary" @click="pingjia"> 提交</el-button>
      </el-form>

    </el-dialog>


    <el-dialog v-model="dialogFormVisible" title="订单详情" width="50%">
      <el-table :data="tableData2"   stripe style="width: 100%">
      <el-table-column label="订单号" width="240">
        <template #default="scope" >
          <el-button type="text" @click="dialogFormVisible = true"
          >{{scope.row.orderId}}</el-button
          >
        </template>
      </el-table-column>
        <el-table-column prop="carPartsName" label="汽车配件名称" width="120"/>
        <el-table-column prop="standard" label="规格" width="120"/>
        <el-table-column prop="carNumber" label="配件编号" width="120"/>
        <!--      <el-table-column prop="buy_price" label="进价" width="120"/>-->
        <el-table-column prop="price" label="价格" width="120"/>
        <el-table-column prop="buy_number" label="购买数量" width="120"/>
        <el-table-column prop="have_number" label="未发数量" width="120"/>
      </el-table>
    </el-dialog>
  </div>

</template>

<script>
import request from "@/utils/request";
import documentCopy from "@element-plus/icons-vue/dist/es/document-copy.mjs"
export default {
  name: "Order_detail",
  components:{
    documentCopy,
  },
  data(){
    let sum_price=0;
    let userId=JSON.parse(sessionStorage.getItem("user")).id;
    return {
      userId,
      tableData: [],
      tableData2:[],
      sum_price,
      dialogFormVisible:false,
      dialogFormVisible2:false,
      form:{},
      order_id:null
    }
  },
  created() {
    this.load()
  },
  methods:{
    pingjia(){
      request.get("/order_form/order_id/"+this.order_id).then(res=>{
        let order_form=res.data
        order_form.evaluate=this.form.value
        order_form.comment=this.form.comment
        request.post("/order_form/save",order_form).then(res=>{
          console.log(res)
          this.$message({
            type:"success",
            message:"评价完成，感谢您的反馈~"
          })
        })
      })
    },
    pringjia2(row){
      this.dialogFormVisible2=true
      this.order_id=row.orderId
    },
    return_goods(row){
      request.get("/order_form/order_id/"+row.orderId).then(res=>{
        let order_form=res.data
        order_form.flag=3
        request.post("/order_form/save",order_form).then(res=>{
          this.$message({
            type:"success",
            message:"退货成功"
          })
          this.load()
        })
      })
    },
    load() {
      request.get("/order_form/user_id/"+this.userId).then(res => {
        console.log(res)
        this.tableData = res.data
      })

    },
    getOrderDetail(row){
      this.dialogFormVisible = true
      request.get("/order_detail/by_orderId/"+row.orderId).then(res=>{
        console.log(res)
        this.tableData2=res.data
      })
    }
  }
}
</script>

<style scoped>
.demo-rate-block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  display: inline-block;
  width: 49%;
  box-sizing: border-box;
}
.demo-rate-block:last-child {
  border-right: none;
}
.demo-rate-block .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
</style>
